#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import wsgiref.handlers
import string
from google.appengine.ext import webapp, db
from db.OrderData import OrderData

import csv
import codecs
import StringIO

from django.http import HttpResponse


class CreateCsv(webapp.RequestHandler):
    def post(self):
        row = []
        csvTitle = ""
        outputKey = self.request.get("outputKey")

        response = HttpResponse(mimetype='application/octet-stream')
        #self.response.headers['content-type'] = 'application/octet-stream;charset=Shift_JIS'
        self.response.headers['content-type'] = 'application/octet-stream;charset=shift_jis'
        self.response.headers['Content-disposition'] = 'attachment;filename=somefilename.csv'

        writer = csv.writer(response)
        
        list = outputKey.split(',')
        for orderKey in list:
            view_datas = db.GqlQuery("select * from OrderData where order_number = :1",
                            orderKey)
            if view_datas.count() > 0:
                csvVal = ""
                view_data = view_datas[0]
                csvVal +=view_data.confirm_status.encode('cp932') + ","
                csvVal +=view_data.confirm_author_title.encode('cp932') + ","
                csvVal +=view_data.operation_dept.encode('cp932') + ","
                csvVal +=view_data.attched_file_name.encode('cp932') + ","
                #csvVal +=view_data.business_conditions)
                csvVal +=view_data.classification.encode('cp932') + ","
                csvVal +=view_data.company_name.encode('cp932') + ","
                csvVal +=view_data.company_service.encode('cp932') + ","
                csvVal +=view_data.company_type.encode('cp932') + ","
                csvVal +=view_data.complete_contact_author.encode('cp932') + ","
                csvVal +=view_data.complete_contact_author_title.encode('cp932') + ","
                csvVal +=view_data.complete_contact_dept.encode('cp932') + ","
                csvVal +=view_data.confirm_author.encode('cp932') + ","
                csvVal +=view_data.confirm_dept.encode('cp932') + ","
                csvVal +=view_data.confirm_message.encode('cp932') + ","
                csvVal +=view_data.content.encode('cp932') + ","
                csvVal +=view_data.effective.encode('cp932') + ","
                csvVal +=view_data.estimate_author.encode('cp932') + ","
                csvVal +=view_data.estimate_contact_author.encode('cp932') + ","
                csvVal +=view_data.estimate_contact_author_title.encode('cp932') + ","
                csvVal +=view_data.estimate_contact_dept.encode('cp932') + ","
                csvVal +=str(view_data.last_modify_time) + ","
                csvVal +=view_data.need_program.encode('cp932') + ","
                csvVal +=view_data.operation_author.encode('cp932') + ","
                csvVal +=view_data.operation_author_title.encode('cp932') + ","
                csvVal +=str(view_data.order_author) + ","
                csvVal +=view_data.order_author_title.encode('cp932') + ","
                csvVal +=view_data.order_content.encode('cp932') + ","
                csvVal +=str(view_data.order_date) + ","
                csvVal +=view_data.order_dept.encode('cp932') + ","
                csvVal +=view_data.order_effect.encode('cp932') + ","
                csvVal +=view_data.order_number.encode('cp932') + ","
                csvVal +=view_data.order_type.encode('cp932') + ","
                #csvVal +=view_data.tempfile_name)
                csvVal +=view_data.company_code.encode('cp932') + ","
                #csvVal +=view_data.tempfile_key)

                #項目名を記述するのが面倒なので、まるごと表示
                if (csvTitle == ""):
                    data1 = view_data._entity
                    dataAll = str(data1).split(',')
                    for datas in dataAll:
                        #タイトルセット
                        data = str(datas).split(':')
                        if(len(data) > 1):
                            #不要なものを削除する
                            setTitle = str(data[0])
                            setTitle = setTitle.replace("u'", "'")
                            setTitle = setTitle.replace("'", '')
                            setTitle = setTitle.replace("{", '')
                            csvTitle += str(setTitle + ",")
                    writer.writerow([csvTitle])
                    #row.append(csvTitle)
                    #writer.writerow([unicode(value, "utf-8") for value in csvTitle])
                #row.append(csvVal)
                #writer.writerow([unicode(value, "utf-8") for value in csvVal])
                csvVal = csvVal + "\r\n"
                #writer.writerow([unicode(value, "utf-8") for value in csvVal])
                writer.writerow([csvVal])
                
        #writer.writerow([row])
        #return response
        #self.response.out.write(response.content)
        #self.response.out.write(response.content)
        #self.response = response
        self.response.out.write(response.content)
        return None
#        return response
